/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.bradipao.deschartsdemo.spinner.util;

import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

/**
 * PixelMap的util工具类
 *
 * @since 2021-08-02
 */
public class PixelMapUtils {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "PixelMapUtils");

    /**
     * Return a drawable object associated with a particular resource ID.
     *
     * <p>Starting in {}, the returned drawable will be styled for the
     * specified Context's theme.</p>
     *
     * @param context The current context
     * @param drawableId The desired resource identifier, as generated by the aapt tool.
     * This integer encodes the package, type, and resource entry.
     * The value 0 is an invalid identifier.
     *
     * @return Drawable An object that can be used to draw this resource.
     */
    public static PixelMapElement getPixelMapElement(Context context, int drawableId) {
        PixelMapElement pixelMapElement;
        PixelMap pixelMap = getPixelMap(context, drawableId);
        if (pixelMap == null) {
            return null;
        } else {
            pixelMapElement = new PixelMapElement(pixelMap);
            return pixelMapElement;
        }
    }

    /**
     * 将drawableId转换为PixelMap
     *
     * @param context 上下文对象
     * @param drawableId ResourceTable资源id
     * @return PixelMap
     */
    public static PixelMap getPixelMap(Context context, int drawableId) {
        PixelMap pixelMap = null;
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (IOException | NotExistException e) {
            HiLog.info(LABEL, "IOException | NotExistException = " + e.toString());
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                HiLog.info(LABEL, "IOException = " + e.toString());
            }
        }
        return pixelMap;
    }
}
